package com.business.system.official.toursism.websiteimage.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.system.official.toursism.websiteimage.entity.WebsiteImage;
import com.business.system.official.toursism.websiteimage.from.WebsiteImageSaveFrom;
import com.business.system.official.toursism.websiteimage.from.WebsiteImageUpdateFrom;
import com.business.system.official.toursism.websiteimage.manager.WebsiteImageManager;
import com.business.system.official.toursism.websiteimage.service.WebsiteImageService;
import com.business.system.official.toursism.websiteimage.vo.data.WebsiteImageDetailData;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.dto.SeedMessageDTO;
import com.seed.core.pojo.service.impl.SeedSystemServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * @description 轮播图
 * @author CodeGenerator
 * @date   2025-01-27
 * @version V1.0
 */
@Slf4j
@Service("websiteImageService")
@Transactional(rollbackFor = ServiceException.class)
public class WebsiteImageServiceImpl extends SeedSystemServiceImpl<WebsiteImageManager, WebsiteImage> implements WebsiteImageService {

    /**
     * 分页查询
     *
     * @param page         分页对象
     * @param queryWrapper 实体对象封装操作类
     * @return 分页对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public IPage<WebsiteImageDetailData> pageVO(IPage<WebsiteImage> page, Wrapper<WebsiteImage> queryWrapper) throws ServiceException {
        IPage<WebsiteImageDetailData> websiteImageDetailDataPage = new Page<>();
        IPage<WebsiteImage> pageList;
        try {
            pageList = this.page(page, queryWrapper);
        } catch (ServiceException e) {
            throw new ServiceException(e);
        }
        websiteImageDetailDataPage.setPages(pageList.getPages());
        websiteImageDetailDataPage.setTotal(pageList.getTotal());
        websiteImageDetailDataPage.setSize(pageList.getSize());
        websiteImageDetailDataPage.setCurrent(pageList.getCurrent());
        try {
            websiteImageDetailDataPage.setRecords(baseManager.listConvertDetailData(pageList.getRecords()));
        } catch (ManagerException e) {
            throw new ServiceException(e);
        }
        return websiteImageDetailDataPage;
    }

    /**
     * 插入一条记录（选择字段，策略插入）
     *
     * @param from 实体对象
     * @return 是否成功
     * @throws ServiceException 业务层异常
     */
    @Override
    public SeedMessageDTO save(WebsiteImageSaveFrom from) throws ServiceException {
        return super.save(this.initSaveFrom(from));
    }

    /**
     * 根据 ID 选择修改
     *
     * @param from 实体对象
     * @return 是否成功
     * @throws ServiceException 业务层异常
     */
    @Override
    public SeedMessageDTO updateById(WebsiteImageUpdateFrom from) throws ServiceException {
        return super.updateById(this.initUpdateFrom(from));
    }

    /**
     * 根据 ID 查询
     *
     * @param id 主键ID
     * @return 对应实体
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteImageDetailData getDetailById(Serializable id) throws ServiceException {
        WebsiteImage entity = super.getById(id);
        if (StringUtils.isEmpty(entity)) {
            return null;
        }
        try {
            return baseManager.convertDetailData(entity);
        } catch (ManagerException e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 初始化保存表单值
     *
     * @param from 表单
     * @return 实体
     */
    private WebsiteImage initSaveFrom(WebsiteImageSaveFrom from) {
        WebsiteImage entity = new WebsiteImage();
        BeanUtils.copyProperties(from, entity);
        return entity;
    }

    /**
     * 初始化更新表单值
     *
     * @param from 表单
     * @return 实体
     */
    private WebsiteImage initUpdateFrom(WebsiteImageUpdateFrom from) {
        WebsiteImage entity = new WebsiteImage();
        BeanUtils.copyProperties(from, entity);
        return entity;
    }

}
